package cn.virens.web.controller.admin.system;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;

import cn.virens.database.mysql.model.sys.SysUser;
import cn.virens.database.service.sys.SysRoleService;
import cn.virens.database.service.sys.SysUserService;
import cn.virens.exception.APIException;
import cn.virens.util.RequestUtil;
import cn.virens.util.page.Page;
import cn.virens.util.shiro.Permission;
import cn.virens.web.common.BaseDataTableController;

/**
 * 账户管理
 * 
 * @作者 : loioi
 * @创建时间 :2017年8月4日 上午11:04:21
 */
@Controller
@RequestMapping("/admin/system/account")
public class SysAccountContorller extends BaseDataTableController<SysUser, SysUserService> {
	private @Resource SysRoleService sysRoleService;

	@Autowired
	public SysAccountContorller(SysUserService service) {
		super("admin/system/account/", "system:account", service);
	}

	@RequestMapping("view.jspx")
	public String view(HttpServletRequest request, HttpServletResponse response, Model model) {
		checkPermission(request, Permission.VIEW.parts(_identity));// 检查用户的权限是否可以访问

		model.addAttribute("record", baseService.getUser(RequestUtil.getValueStr(request, "account")));

		return returnUrl("view", model);
	}

	@RequestMapping("add.jspx")
	public String add(HttpServletRequest request, HttpServletResponse response, Model model) throws APIException {
		checkPermission(request, Permission.MANAGE.parts(_identity));// 检查用户的权限是否可以访问

		model.addAttribute("_roles", sysRoleService.selectAll(null));

		return returnUrl("add", model);
	}

	@RequestMapping("edit.jspx")
	public String edit(HttpServletRequest request, HttpServletResponse response, Model model) throws APIException {
		model.addAttribute("_oper", "edit");
		model.addAttribute("_type", "修改");

		model.addAttribute("record", baseService.getUser(RequestUtil.getValueStr(request, "account")));

		return add(request, response, model);
	}

	@Override
	protected PageInfo<?> getData(HttpServletRequest request, Page page) throws Exception {
		page.addParam("name", RequestUtil.getValueStr(request, "name", null));
		page.addParam("account", RequestUtil.getValueStr(request, "account", null));

		return super.getData(request, page);
	}
}
